﻿using ArchivingDataModel;
using ArchivingDataWebForms.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace ArchivingDataWebForms.Pages.Organization
{
    public partial class WebFormOrganization : System.Web.UI.page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.getRootOrganizations(null);
            }
        }

        protected void RadButtonPrimary_onClick(object sender, EventArgs e)
        {

        }
        protected void RadButtonSecondary_onClick(object sender, EventArgs e)
        {

        }
        protected void RadButtonModify_onClick(object sender, EventArgs e)
        {

        }
        protected void RadButtonRemove_onClick(object sender, EventArgs e)
        {

        }
        protected void RadTreeViewOrganizations_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                e.Node.Nodes.Clear();
                long parentID = Convert.ToInt64(e.Node.Value);
                var childFolders = ent.Folders.Where(i => i.ParentFolder_ID == parentID && i.Is_RecycleBin == false).OrderBy(i => i.Folder_Order).ToArray();
                for (int i = 0; i < childFolders.Length; i++)
                {
                    //string url = ImageEdittingHandler.GetUrl(childFolders[i].ImagePath, 32, 32, null, null, null, FlipType.None);
                    RadTreeNode Node = new RadTreeNode()
                    {
                        //Text = childFolders[i].FolderName_Ar + " (" + this.getFilesNumber(ent, childFolders[i]) + ")",
                        Value = childFolders[i].Folder_ID.ToString(),
                        ExpandMode = TreeNodeExpandMode.ServerSide,
                        //ImageUrl = url,
                    };
                    e.Node.Nodes.Add(Node);

                    long id = childFolders[i].Folder_ID;
                    //remove expand if no child
                    if (!ent.Folders.Any(k => k.ParentFolder_ID == id && k.Is_RecycleBin == false))
                    {
                        Node.Expanded = true;
                    }

                }

            }
        }


        private void getRootOrganizations(long? id)
        {
            try
            {
                using (ArchivingDataEntities ent = new ArchivingDataEntities())
                {
                    this.RadTreeViewOrganizations.Nodes.Clear();
                    var allorgs = ent.Organizations.Where(i => i.Is_External==0 || i.Is_External==null).ToArray();
                    var rootOrgs = allorgs.Where(i => i.Organization_Parent_ID ==null).ToArray();

                    for (int i = 0; i < rootOrgs.Length; i++)
                    {
                        RadTreeNode rootNode = new RadTreeNode()
                        {
                            Text = rootOrgs[i].Organization_Number+" - "+ rootOrgs[i].Organization_Name_AR ,
                            Value = rootOrgs[i].Organization_ID.ToString(),
                            ExpandMode = TreeNodeExpandMode.ServerSide,
                        };
                        this.RadTreeViewOrganizations.Nodes.Add(rootNode);

                        //remove expand if no child
                        if (!allorgs.Any(k => k.Organization_Parent_ID == rootOrgs[i].Organization_ID))
                        {
                            rootNode.Expanded = true;
                        }
                    }

                    //select node from querystring
                    if (id.HasValue)
                    {
                        this.bindRequestNodeChain(allorgs, id.Value);
                    }

                }
            }
            catch (Exception er)
            {
                //this.ShowNotification("حدث الخطا التالي اثناء الحفظ " + er.Message, NotificationIconType.Error);
            }
        }

        protected void bindRequestNodeChain(ArchivingDataModel.Organization[] allOrgs, long id)
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                ArchivingDataModel.Organization currentOrganization = allOrgs.FirstOrDefault(i => i.Organization_ID == id);
                List<ArchivingDataModel.Organization> chain = new List<ArchivingDataModel.Organization>();
                while (currentOrganization.Organization_Parent_ID.HasValue)
                {
                    ArchivingDataModel.Organization parent = allOrgs.FirstOrDefault(i => i.Organization_ID == currentOrganization.Organization_Parent_ID);
                    chain.Insert(0, parent);
                    currentOrganization = parent;
                }
                //get chain of node
                for (int i = 0; i < chain.Count; i++)
                {
                    RadTreeNode node = this.RadTreeViewOrganizations.FindNodeByValue(chain[i].Organization_ID.ToString());

                    long parentID = chain[i].Organization_ID;
                    var childOrganizations = allOrgs.Where(k => k.Organization_Parent_ID == parentID && k.Is_External == 0).ToArray();
                    for (int k = 0; k < childOrganizations.Length; k++)
                    {
                        RadTreeNode childnode = new RadTreeNode()
                        {
                            Text = childOrganizations[k].Organization_Number + " - " + childOrganizations[k].Organization_Name_AR,
                            Value = childOrganizations[k].Organization_ID.ToString(),
                            ExpandMode = TreeNodeExpandMode.ServerSide,
                        };
                        node.Nodes.Add(childnode);

                        long childID = childOrganizations[k].Organization_ID;
                        //remove expand if no child
                        if (!allOrgs.Any(z => z.Organization_Parent_ID == childID && z.Is_External == 0))
                        {
                            childnode.Expanded = true;
                        }

                    }
                    node.Expanded = true;
                }
                var selectedNode = this.RadTreeViewOrganizations.FindNodeByValue(id.ToString());
                selectedNode.Selected = true;
                selectedNode.ExpandParentNodes();

                ////remove expand if no child
                //if (!allFoldsers.Any(k => k.ParentFolder_ID == id && k.Is_RecycleBin == false))
                //{
                //    selectedNode.Expanded = true;
                //}
            }
        }
    }
}